﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Network
* Version: 0.5
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["phpp_network_g"] = new Object();
myPlugins["phpp_network_g"]["name"] = getLocalizedString2("mod_sysm_plugin_net_name");
myPlugins["phpp_network_g"]["onlyonce"] = false;
myPlugins["phpp_network_g"]["newObject"] = function (NewSettings) { return new plugin_phpp_network_g_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_phpp_network_g_sensor(NewSettings) {
	// Required variables
	this.plugin = "phpp_network_g";
	this.settings = NewSettings;

	this.edit = plugin_network_edit;
	this.ReDraw = plugin_network_ReDraw;
	this.Update = plugin_network_update;

	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;
	var obj = this;




	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolorI) this.settings.barcolorI = "#008F00";
	if(!this.settings.barcolorO) this.settings.barcolorO = "#8F0000";
	if(!this.settings.NetID) this.settings.NetID = 0;
	if(!this.settings.label) this.settings.label = "";
	if(!this.settings.labelright) this.settings.labelright = 0;
	if(!this.settings.stats) {
		this.settings.stats = new Object();
		this.settings.stats["now"] = new Object();
		this.settings.stats["now"].ValueI = 0;
		this.settings.stats["now"].ValueO = 0;
		this.settings.stats["hourly"] = new Object();
		this.settings.stats["hourly"].last = -1;
		this.settings.stats["hourly"].ValueI = 0;
		this.settings.stats["hourly"].ValueO = 0;
		this.settings.stats["hourly"].LastValueI = 0;
		this.settings.stats["hourly"].LastValueO = 0;
		this.settings.stats["daily"] = new Object();
		this.settings.stats["daily"].last = -1;
		this.settings.stats["daily"].ValueI = 0;
		this.settings.stats["daily"].ValueO = 0;
		this.settings.stats["daily"].LastValueI = 0;
		this.settings.stats["daily"].LastValueO = 0;
		this.settings.stats["weekly"] = new Object();
		this.settings.stats["weekly"].last = -1;
		this.settings.stats["weekly"].ValueI = 0;
		this.settings.stats["weekly"].ValueO = 0;
		this.settings.stats["weekly"].LastValueI = 0;
		this.settings.stats["weekly"].LastValueO = 0;
		this.settings.stats["monthly"] = new Object();
		this.settings.stats["monthly"].last = -1;
		this.settings.stats["monthly"].ValueI = 0;
		this.settings.stats["monthly"].ValueO = 0;
		this.settings.stats["monthly"].LastValueI = 0;
		this.settings.stats["monthly"].LastValueO = 0;
	}
	if(!this.settings.id)
		for(var i in PHPSysInfo)
			if(i) {
				this.settings.id = i;
				//break;
			}

	this.settings.stats["now"].ValueI = 0;
	this.settings.stats["now"].ValueO = 0;

	this.NetW = new Object();
	this.NetW["lasttime"] = 0;
	this.NetW["peakI"] = 1;
	this.NetW["peakO"] = 1;
	this.NetW["diffI"] = 0;
	this.NetW["diffO"] = 0;


	//The Sensor
	this.networkI = new class_PercentBar(this.frame,0,0,"Network I");
	this.networkI.PercentColor(this.settings.color);
	this.networkI.PercentBarColor(this.settings.barcolorI);

	this.networkO = new class_PercentBar(this.frame,0,mod_sysmonitor_size,"");
	this.networkO.PercentColor(this.settings.color);
	this.networkO.PercentBarColor(this.settings.barcolorO);

	plugin_network_update.call(obj);

	// Calculating sensore height (required!)
	this.frame.height = this.networkO.background.vOffset + this.networkO.background.height;

	return true;

// --------------------------------------------------------------------------------
// Helper Functions


	function plugin_network_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_incolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolorI;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_outcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolorO;


		var NetOpt = Array();
		var NetVal = Array();
		for(var i = 0; i < PHPSysInfo[this.settings.id]["NetDevice"].length ; i++) {
			NetVal[i] = i;
			NetOpt[i] = PHPSysInfo[this.settings.id]["NetDevice"][i]["Name"];
		}

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_device");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = NetOpt;
		Drive_Edit_DialogFields[FieldID].optionValue = NetVal;
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.NetID;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_label");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.label;
		Drive_Edit_DialogFields[FieldID].description = getLocalizedString2("mod_sysm_plugin_net_pref_labeldesc");

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_net_pref_text2");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = new Array(getLocalizedString2("mod_sysm_plugin_net_pref_cur"),getLocalizedString2("mod_sysm_plugin_net_pref_hourly"),getLocalizedString2("mod_sysm_plugin_net_pref_daily"),getLocalizedString2("mod_sysm_plugin_net_pref_weekly"),getLocalizedString2("mod_sysm_plugin_net_pref_monthly"));
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(0,1,2,3,4);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.labelright;
		Drive_Edit_DialogFields[FieldID].hidden = 1;

		var SList = new Array();
		var SVal = new Array();
		for(var i in PHPSysInfo) {
			SList[SList.length] = PHPSysInfo[i]["Vitals"]["Hostname"];
			SVal[SVal.length] = i;
		}
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_php_host_source");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = SList;
		Drive_Edit_DialogFields[FieldID].optionValue = SVal;
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.id;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.settings.barcolorI = Drive_Edit_DialogResults[1];
			this.settings.barcolorO = Drive_Edit_DialogResults[2];
			this.networkI.PercentColor(this.settings.color);
			this.networkO.PercentColor(this.settings.color);
			this.networkI.PercentBarColor(this.settings.barcolorI);
			this.networkO.PercentBarColor(this.settings.barcolorO);

			if(this.settings.NetID != Drive_Edit_DialogResults[3]) {
				this.settings.NetID = Drive_Edit_DialogResults[3];
				this.NetW["lasttime"] = 0;
				this.NetW["peakI"] = 1;
				this.NetW["peakO"] = 1;
				this.NetW["diffI"] = 0;
				this.NetW["diffO"] = 0;

				this.settings.stats["now"].ValueI = 0;
				this.settings.stats["now"].ValueO = 0;
				this.settings.stats["hourly"].last = -1;
				this.settings.stats["hourly"].ValueI = 0;
				this.settings.stats["hourly"].ValueO = 0;
				this.settings.stats["hourly"].LastValueI = 0;
				this.settings.stats["hourly"].LastValueO = 0;
				this.settings.stats["daily"].last = -1;
				this.settings.stats["daily"].ValueI = 0;
				this.settings.stats["daily"].ValueO = 0;
				this.settings.stats["daily"].LastValueI = 0;
				this.settings.stats["daily"].LastValueO = 0;
				this.settings.stats["weekly"].last = -1;
				this.settings.stats["weekly"].ValueI = 0;
				this.settings.stats["weekly"].ValueO = 0;
				this.settings.stats["weekly"].LastValueI = 0;
				this.settings.stats["weekly"].LastValueO = 0;
				this.settings.stats["monthly"].last = -1;
				this.settings.stats["monthly"].ValueI = 0;
				this.settings.stats["monthly"].ValueO = 0;
				this.settings.stats["monthly"].LastValueI = 0;
				this.settings.stats["monthly"].LastValueO = 0;
			}

			this.settings.label = Drive_Edit_DialogResults[4];
			this.settings.labelright = Drive_Edit_DialogResults[5];
			this.settings.id = Drive_Edit_DialogResults[6];

			plugin_network_update.call(obj);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}


	function plugin_network_update() {
		if(PHPSysInfo[this.settings.id]) {
			if(this.settings.label!="")
				this.networkI.text_name.data = this.settings.label.substr(0,16);
			else
				this.networkI.text_name.data = PHPSysInfo[this.settings.id]["NetDevice"][this.settings.NetID]["Name"].substr(0,16);

			if( this.NetW["lasttime"] < PHPSysInfo[this.settings.id]["Generation"]["timestamp"] ) {
				if( this.NetW["lasttime"] <= 0 ) this.NetW["lasttime"] = PHPSysInfo[this.settings.id]["Generation"]["timestamp"] - 1;
				this.NetW["timediff"] = PHPSysInfo[this.settings.id]["Generation"]["timestamp"] - this.NetW["lasttime"];
				this.NetW["lasttime"] = PHPSysInfo[this.settings.id]["Generation"]["timestamp"];

				//Initial Values
				if(this.settings.stats["now"].ValueI <= 0) this.settings.stats["now"].ValueI = PHPSysInfo[this.settings.id]["NetDevice"][this.settings.NetID]["RxBytes"];
				if(this.settings.stats["now"].ValueO <= 0) this.settings.stats["now"].ValueO = PHPSysInfo[this.settings.id]["NetDevice"][this.settings.NetID]["TxBytes"];
	
				this.NetW["diffI"] = PHPSysInfo[this.settings.id]["NetDevice"][this.settings.NetID]["RxBytes"] - this.settings.stats["now"].ValueI;
				this.NetW["diffO"] = PHPSysInfo[this.settings.id]["NetDevice"][this.settings.NetID]["TxBytes"] - this.settings.stats["now"].ValueO;
	
				this.settings.stats["now"].ValueI = PHPSysInfo[this.settings.id]["NetDevice"][this.settings.NetID]["RxBytes"];
				this.settings.stats["now"].ValueO = PHPSysInfo[this.settings.id]["NetDevice"][this.settings.NetID]["TxBytes"];
	

				//Degrading Peal Value
				if( this.NetW["diffI"]*2 < this.NetW["peakI"] ) this.NetW["peakI"] -= parseInt(this.NetW["diffI"]/10);
				if( this.NetW["diffO"]*2 < this.NetW["peakO"] ) this.NetW["peakO"] -= parseInt(this.NetW["diffO"]/10);
				if( this.NetW["diffI"]*100 < this.NetW["peakI"] ) this.NetW["peakI"] = this.NetW["diffI"]*100;
				if( this.NetW["diffO"]*100 < this.NetW["peakO"] ) this.NetW["peakO"] = this.NetW["diffO"]*100;

				//Set Peak Value
				if( this.NetW["diffI"] > this.NetW["peakI"] ) this.NetW["peakI"] = this.NetW["diffI"];
				if( this.NetW["diffO"] > this.NetW["peakO"] ) this.NetW["peakO"] = this.NetW["diffO"];
			}

			//Update
			switch (this.settings.labelright) {
				case "1":
					this.networkI.PercentBarValue(this.NetW["diffI"] / this.NetW["peakI"] * 100, bytesToUIString(  this.settings.stats["hourly"].ValueI   ) );
					this.networkO.PercentBarValue(this.NetW["diffO"] / this.NetW["peakO"] * 100, bytesToUIString(  this.settings.stats["hourly"].ValueO  ) );
				break;
				case "2":
					this.networkI.PercentBarValue(this.NetW["diffI"] / this.NetW["peakI"] * 100, bytesToUIString(  this.settings.stats["daily"].ValueI   ) );
					this.networkO.PercentBarValue(this.NetW["diffO"] / this.NetW["peakO"] * 100, bytesToUIString(  this.settings.stats["daily"].ValueO  ) );
				break;
				case "3":
					this.networkI.PercentBarValue(this.NetW["diffI"] / this.NetW["peakI"] * 100, bytesToUIString(  this.settings.stats["weekly"].ValueI   ) );
					this.networkO.PercentBarValue(this.NetW["diffO"] / this.NetW["peakO"] * 100, bytesToUIString(  this.settings.stats["weekly"].ValueO  ) );
				break;
				case "4":
					this.networkI.PercentBarValue(this.NetW["diffI"] / this.NetW["peakI"] * 100, bytesToUIString(  this.settings.stats["monthly"].ValueI   ) );
					this.networkO.PercentBarValue(this.NetW["diffO"] / this.NetW["peakO"] * 100, bytesToUIString(  this.settings.stats["monthly"].ValueO  ) );
				break;
				default:
					this.networkI.PercentBarValue(this.NetW["diffI"] / this.NetW["peakI"] * 100, bytesToUIString(  this.NetW["diffI"] / this.NetW["timediff"] ) );
					this.networkO.PercentBarValue(this.NetW["diffO"] / this.NetW["peakO"] * 100, bytesToUIString(  this.NetW["diffO"] / this.NetW["timediff"] ) );
				break;
			}


		}
		return true;
	}



	function plugin_network_ReDraw() {
		this.frame.width = mod_sysmonitor_width;

		this.networkI.style = preferences.mod_sysmonitor_bar_style.value;
		this.networkI.size = mod_sysmonitor_size;
		this.networkO.style = preferences.mod_sysmonitor_bar_style.value;
		this.networkO.size = mod_sysmonitor_size;
		this.networkO.vOffset = mod_sysmonitor_size;

		// Re-Calculating sensore height (with background)
		this.frame.height = this.networkO.background.vOffset + this.networkO.background.height;

		return true;
	}



}



// --------------------------------------------------------------------------------
// The END